const { FileUtil } = require('../../regist')
const path = require('path');
const moment = require('moment');
const {File} = FileUtil;

class LogService {

	constructor(logPath){
		this.logPath = logPath;
		//日志目录
		this.directory = new File(logPath)
		if(!this.directory.exist()){
			this.directory.mkdir();
		}
	}

	//need ovrride
	getLogFile(){

	}

	appendJson(data){
		this.getLogFile().append(JSON.stringify(data)+"\n");
	}
}

//交易日志服务
class TxLogService extends LogService {

	constructor(logPath){
		super(logPath)
	}

	getLogFile(){
		const time = moment().format("YYYY-MM")
		const logFile = new File(path.join(this.logPath,`${time}.log`))
		logFile.touch();
		return logFile;
	}

	//下单成功
	orderSuccess(data){
		data.date = moment().format("YYYY-MM-DD HH:mm:ss")
		console.log("下单信息:",data);
		this.appendJson(data)
	}


}

//系统日志服务
class SysLogService extends LogService {

	constructor(logPath){
		super(logPath)
	}

	getLogFile(){
		const logFile = new File(path.join(this.logPath,`system.log`))
		logFile.touch();
		return logFile;
	}

}

module.exports = {TxLogService,SysLogService}